package tech.lingrui.mnpanonymousservice.config;


import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * web socket 配置
 *
 * @author lqs2
 * @date 2018/11/25, Sun
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {


    /**
     * 注册 Stomp的端点
     * addEndpoint：添加STOMP协议的端点。提供WebSocket或SockJS客户端访问的地址
     * withSockJS：使用SockJS协议
     *
     * @param registry 我不知道
     **/
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // 广播式
        registry.addEndpoint("/sample")
                // 添加允许跨域访问
                .setAllowedOrigins("*")
                .withSockJS();

        // 点对点式
        registry.addEndpoint("/chat")
                // 添加允许跨域访问
                .setAllowedOrigins("*")
                .withSockJS();
    }

    /**
     * 配置消息代理
     * 启动Broker，消息的发送的地址符合配置的前缀来的消息才发送到这个broker
     *
     * @param registry 我不知道
     **/
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        // 广播式，消息代理， 推送消息前缀
        registry.enableSimpleBroker("/topic");

        //点对点式应配置/queue和/topic消息代理
        registry.enableSimpleBroker("/queue","/topic");


        // 应用请求前缀
//        registry.setApplicationDestinationPrefixes("/api/v1/socket/req");
        // 推送用户前缀
//        registry.setUserDestinationPrefix("/user");
    }

}
